package com.poetize.plus.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.poetize.plus.entity.dto.SortDTO;
import com.poetize.plus.entity.dto.page.SortPageDTO;
import com.poetize.plus.entity.vo.SelectVO;
import com.poetize.plus.entity.vo.page.SortPageVO;
import com.poetize.plus.entity.vo.SortVO;
import com.poetize.plus.service.SortService;
import com.poetize.plus.utils.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author zuosy
 * @Date 2024/3/30 14:39
 **/
@Tag(name = "分类信息")
@RestController
@RequestMapping("/admin/sort")
@RequiredArgsConstructor
public class SortController {

    private final SortService sortService;

    @GetMapping("/sortPage")
    @Operation(summary = "分类分页数据")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Page<SortPageVO>> sortPage(SortPageDTO sortPageDTO){
        return R.ok(sortService.sortPage(sortPageDTO));
    }

    @PostMapping("/addSort")
    @Operation(summary = "新增分类信息")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> addSort(@RequestBody @Validated(Insert.class) SortDTO sortDTO){
        return R.ok(sortService.addSort(sortDTO));
    }

    @GetMapping("/{id}")
    @Operation(summary = "分类信息详情")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<SortVO> querySort(@PathVariable("id") Long id){
        return R.ok(sortService.querySort(id));
    }

    @PostMapping("/updateSort")
    @Operation(summary = "修改分类信息")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> updateSort(@RequestBody @Validated(Update.class) SortDTO sortDTO){
        return R.ok(sortService.updateSort(sortDTO));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除分类信息")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> deleteSort(@PathVariable("id") Long id){
        return R.ok(sortService.deleteSort(id));
    }

    @GetMapping("/sortSelect")
    @Operation(summary = "分类下拉框")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<List<SelectVO>> sortSelect(){
        return R.ok(sortService.sortSelect());
    }
}
